package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.RoleMapper;
import com.gtq.uhome.mapper.StaffMapper;
import com.gtq.uhome.mapper.StaffRoleMapper;
import com.gtq.uhome.pojo.Staff;
import com.gtq.uhome.service.OperatorService;
import com.gtq.uhome.util.MD5Creater;
import com.gtq.uhome.vo.Operator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 操作员
 */
@Service("operatorService")
@Transactional(propagation = Propagation. REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
public class OperatorServiceImpl implements OperatorService {

    @Resource
    StaffMapper  staffMapper;

    @Resource
    StaffRoleMapper staffRoleMapper;

    @Resource
    RoleMapper roleMapper;

    /**
     * 查询操作员相关信息
     * @return List<Operator> 结果集
     */
    @Override
    public List<Operator> selectAll() {
        List<Operator> operators = staffMapper.selectOperator();
        return operators;
    }

    /**
     * 根据条件查询操作员相关信息
     * @param operator
     * @return List<Operator> 结果集
     */
    @Override
    public List<Operator> selectAllLike(Operator operator) {
        //通过条件进行模糊查询
        List<Operator> operators = staffMapper.selectOperatorLike(operator);
        return operators;
    }

    /**
     * 添加操作员，密码通过MD5进行加密
     * @param operator
     * @return
     */
    @Override
    @Transactional
    public Integer insertStaffRole(Operator operator) {
        //密码通过MD5加密
        operator.setStaffPassword("123456");
        //先添加staff表中信息
        Integer staff = staffMapper.insertStaffRole(operator);
        //添加成功再执行关联表信息添加
        if (staff==1){
           /* System.out.println("开始执行查询");*/
            //获取员工ID
            Integer byId = findId(operator);
            operator.setStaffId(byId);
            //添加关联表信息
            Integer staffRole = staffRoleMapper.insertStaffRole(operator);
        return staffRole;
        }
        return 0;
    }

    /**
     * 查询staff表中staffId
     * @param object
     * @return
     */
    @Override
    public Integer findId(Object object) {
        Integer byId = staffMapper.findId(object);
        return byId ;
    }

    /**
     * 修改操作员信息，分别修改staff表和关联表中的信息及状态
     * @param operator
     * @return Integer 影响的行数
     */
    @Override
    @Transactional
    public Integer updateAll(Operator operator) {
        Integer integer = staffMapper.updateAll(operator);
        if (integer==1){
                Integer role = roleMapper.updateRoleName(operator);
                return role;
        }
        return 0;
    }

    /**
     * 实现假删除
     * @param
     * @return Integer 影响的行数
     */
    @Override
    @Transactional
    public Integer deleteAll(List<Operator> operators) {
        Integer staffRole=0;
        for (Operator operator : operators) {
            staffRole = staffRoleMapper.upDateRole(operator);
        }
        return staffRole;
    }

    @Override
    @Transactional
    public Integer updatePassword(Staff staff) {
        staff.setStaffPassword("123456");
        int i = staffMapper.resetPassword(staff);
        return i;
    }


}
